﻿using System;
using System.Web;
using Newtonsoft.Json;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;
using eCommerce.log;

namespace eCommerce.db.Action
{
    public class AllSystemAction : IBaseAction
    {
        public void Do(HttpContext context)
        {
            var actionName = Utils.GetIdFromRequestString(Utils.Action, string.Empty);

            var responseData = new ResponseData();
            switch (actionName)
            {
                case "calc_size_quantity":
                    responseData = CalcSizeAndQuantityStoreSubOrderByStoreSizeProduct();
                    break;
                
                case "delete":
                    break;

                case "":
                    responseData.Success = false;
                    responseData.Message = "Can not found this action.";
                    break;
            }

            var jSonString = JsonConvert.SerializeObject(responseData, Utils.SerializeFormat("MM/dd/yyyy HH:mm:ss"));
            if (!string.IsNullOrEmpty(context.Request.Params["callback"]))
                jSonString = string.Format("{0}({1})", context.Request.Params["callback"], jSonString);
            context.Response.Write(jSonString);
        }

        private static ResponseData CalcSizeAndQuantityStoreSubOrderByStoreSizeProduct()
        {
            var responseData = new ResponseData();

            var storeId     = Utils.GetIdFromRequestString(Utils.UserName, 0);
            var productId   = Utils.GetIdFromRequestString(Utils.ProductId, 0);

            try
            {
                AllSystemBo.CalcSizeAndQuantityStoreSubOrderByStoreSizeProduct(storeId, productId);

                responseData.Success  = true;
                responseData.Data     = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }        
    }
}
